<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				customerCacheKey:'rps-tz-customerInfomationAdjustment-customer-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:'temployee/findUserById'
					},
					{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'283',
							categoryId:'6431'
						}
					}
					
				]
				if(fileId){
					ajaxArr.push({
						url:'customerInformationAdjustment/getValue',
						param:{
							fileId: fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posName"),mainData)
					changeEditor("title1","isPotentialCustomerId","data",configSelectData(res[2],"id","text"),mainData)
					changeEditor("title1","sharingInstId","data",configSelectData(res[2],"id","text"),mainData)
					changeEditor("title1","customerId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({posPrimary: true}),
							url:'customerInformationAdjustment/getCustomer',
							namefield:'customerName',
							extrafield:"customerCode",
							notefield:'customerShortName',
							initload:true,
							searchfield:'search',
							cachename:_that.customerCacheKey
						}
					},null,mainData)
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for (let index in res[3].value) {
							if (fieldArr.includes(index)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, index, res[3].value[index])
								_that.$set(_that.formdata, index, res[3].value[index])
							}
						}
						if(_that.$refs.autoform.formdata.customerName){
							changeEditor("title1","customerName","type",'textarea',mainData)
						}
						if(_that.$refs.autoform.formdata.customerShortName){
							changeEditor("title1","customerShortName","type",'textarea',mainData)
						}
						if(_that.$refs.autoform.formdata.customerCode2){
							changeEditor("title1","customerCode2","type",'textarea',mainData)
						}
					}
					_that.$nextTick(function(){
						if(res[1].value){
							_that.$refs.autoform.formdata.customerOwnerId = res[1].value.id
							_that.$refs.autoform.formdata.customerOwnerName = res[1].value.username
							_that.$refs.autoform.formdata.postId = res[1].value.salaryPosInstId
							_that.$refs.autoform.formdata.postName = res[1].value.salaryPosInstName
							_that.$refs.autoform.formdata.instId = res[1].value.salaryPosInstId
							_that.$refs.autoform.formdata.instName = res[1].value.salaryPosInstName
						}
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("title1","isPotentialCustomerId")
						_that.$refs.autoform.initSelectIndex("title1","sharingInstId")
					})
				},options)
			},
			formSubmit() {
				let _that = this
				if(!_that.edit){
					_that.om.formSave("customerInformationAdjustment/doAdd",_that.formdata,_that)
				}else{
					_that.om.formSave("customerInformationAdjustment/doUpdate",_that.formdata,_that)
				}
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.customerCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerId = d.customerId
							formdata.customerCode = d.customerCode
							formdata.customerName = d.customerName
							_that.$refs.autoform.changeEditor("title1","customerName","type",'textarea')
							formdata.customerShortName = d.customerShortName
							_that.$refs.autoform.changeEditor("title1","customerShortName","type",'textarea')
							formdata.customerCode2 = d.customerId
							_that.$refs.autoform.changeEditor("title1","customerCode2","type",'textarea')
							uni.removeStorage({
								key:_that.customerCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
       